import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import eslintPlugin from "vite-plugin-eslint";
import lessFunctionsPlugin from "less-plugin-functions";
import { resolve } from "path";
import viteCompression from "vite-plugin-compression";
import { visualizer } from "rollup-plugin-visualizer";
import legacyPlugin from "@vitejs/plugin-legacy";

// https://v3.vitejs.dev/config/#config-intellisense
export default defineConfig(({ mode }: any) => {
  // console.log("mode", mode);
  return {
    base: `/${loadEnv(mode, "./").VITE_MODULE_NAME}/`,
    resolve: {
      alias: {
        "@": resolve(__dirname, "./src"),
      },
    },
    server: {
      host: process.env.NODE_ENV !== "production",
      proxy: {
        "/ret-information/report": {
          target: "https://retmgw.nesc.cn:8085",
        },
        "/reqXml": {
          target: "http://222.168.95.187:7773",
        },
        "/reqsavemap": {
          target: "http://222.168.95.187:7773",
        },
        "/reqreadmap": {
          target: "http://222.168.95.187:7773",
        },
        "/reqsavefile": {
          target: "http://222.168.95.187:7773",
        },
        "/reqreadfile": {
          target: "http://222.168.95.187:7773",
        },
      },
    },
    plugins: [
      vue(),
      eslintPlugin({
        include: ["src/**/*.ts", "src/**/*.vue", "src/*.ts", "src/*.vue"],
      }),
      viteCompression({
        // 超过 10kB 的才压缩
        threshold: 10240,
      }),
      // 打包分析工具，分析时可以放开看看
      // visualizer({
      //   open: true, // 注意这里要设置为true，否则无效
      //   gzipSize: true, // 分析图生成的文件名
      //   brotliSize: true, // 收集 brotli 大小并将其显示
      //   filename: "stats.html", // 分析图生成的文件名
      // }),
      // 兼容低版本浏览器 https://cn.vitejs.dev/guide/build.html#browser-compatibility
      legacyPlugin({
        targets: ["> 0%", "Android >= 5", "IOS >= 9", "ie >= 9", "Chrome >= 30"], // 需要兼容的目标列表，可以设置多个
        additionalLegacyPolyfills: ["regenerator-runtime/runtime"], // 面向IE11时需要此插件
        polyfills: ["es.promise.finally", "es/map", "es/set"],
        modernPolyfills: ["es.promise.finally"],
      }),
    ],
    build: {
      // 兼容安卓微信中的 webview
      cssTarget: "chrome61",
      // 代码压缩方式
      minify: "terser",
      rollupOptions: {
        output: {
          chunkFileNames: "js/[name]-[hash].js",
          entryFileNames: "js/[name]-[hash].js",
          manualChunks: {
            // 拆分代码，将部分公共包拆分出来，减少index.js 文件提交，配置完后自动按需加载。
            vue: ["vue", "vue-router"],
            vant: ["vant"],
            lodash: ["lodash"],
            "sa-sdk-javascript": ["sa-sdk-javascript"],
          },
          assetFileNames: (assetInfo) => {
            let extType = "";
            if (assetInfo && assetInfo.name) {
              const info = assetInfo.name;
              if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(info)) {
                extType = "img";
              }
            }
            if (assetInfo && assetInfo.name) {
              const info = assetInfo.name;
              if (/\.(css)(\?.*)?$/.test(info)) {
                extType = "css";
              }
            }
            return extType + "/[name]-[hash].[ext]";
          },
        },
      },
    },
    // 生产环境移除 console/debugger
    esbuild: {
      drop: mode === "production" ? ["console", "debugger"] : [],
    },
    // 配置css 全局注入
    css: {
      preprocessorOptions: {
        less: {
          charset: false,
          additionalData: '@import (reference) "@/assets/styles/global.less";',
          // 自定义函数插件， rem、vw 转换函数
          plugins: [new lessFunctionsPlugin()],
        },
      },
    },
  };
});
